package com.herui.iot.ui.evaluation;

import android.util.Log;

import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sdyu_lib.utils.StringUtils;
import com.herui.sport_care.BasePresenter;
import com.herui.sport_care.BaseView;
import com.herui.sport_care.data.RepositoryFactory;
import com.herui.sport_care.data.eval.AgreementInfo;
import com.herui.sport_care.data.eval.BetaSchemeInfo;
import com.herui.sport_care.data.eval.EvalDetail;
import com.herui.sport_care.data.eval.EvalHostoryInfo;
import com.herui.sport_care.data.eval.EvalInfo;
import com.herui.sport_care.data.eval.OrganTreeInfo;
import com.herui.sport_care.data.eval.TestUserInfo;
import com.herui.sport_care.data.eval.source.EvalSource;
import com.herui.sport_care.data.user.UserInfo;
import com.herui.sport_care.request.SortType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by amanda.shan on 2022/12/4.
 */
public class EvalPresenter implements BasePresenter {

    static final String TAG = "EvalPresenter";

    public static interface View extends BaseView{
        // 回调当日评估列表
        default void onDayEvalInfo(List<EvalInfo> result){}

        // 回调历史评估列表
        default void onEvalHostoryInfo(List<EvalHostoryInfo> result){}
        // 回调组织架构
        default void onOrganTreeInfo(OrganTreeInfo organTreeInfo){}
        // 回调测试方案
        default void onBetaSchemeInfo(List<BetaSchemeInfo> betaSchemeInfos){}
        // 回调测试协议
        default void onAgreementInfo(List<AgreementInfo> agreementInfos){}
        // 回调评估详情
        default void onEvalDetail(EvalDetail evalDetail){}
        // 回调测试人员列表
        default void onTestUserInfo(List<TestUserInfo> testUserInfos){}
        // 回调功率
        default void onPower(Double value){}
        // 创建成功
        default void onCreateSucess(){}
        default void onNameError(){}
    }

    private View view;
    private EvalSource evalSource;

    public EvalPresenter(View view) {
        this.view = view;
        evalSource = RepositoryFactory.getInstance().getEvalSource();
    }

    /**
     * 获取当日评估
     */
    public void getEvalDay(){
        Log.d(TAG, "getEvalDay: 获取当日评估");
        evalSource.getEvalDay(new Callback.CommonCallback<List<EvalInfo>>() {
            @Override
            public void onSuccess(List<EvalInfo> result) {
                view.onDayEvalInfo(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取组织架构
     */
    public void getOrganTreeList(){
        UserInfo userInfo = RepositoryFactory.getInstance().getUserSource().getUserInfo();
        evalSource.getOrganTreeList(userInfo.getOrganId(), new Callback.CommonCallback<OrganTreeInfo>() {
            @Override
            public void onSuccess(OrganTreeInfo result) {
                view.onOrganTreeInfo(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取测试方案
     */
    public void getBetaScheme(){
        evalSource.getBetaScheme(new Callback.CommonCallback<List<BetaSchemeInfo>>() {
            @Override
            public void onSuccess(List<BetaSchemeInfo> result) {
                view.onBetaSchemeInfo(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取测试协议
     */
    public void getAgreement(Long betaSchemeId){
        evalSource.getAgreement(betaSchemeId, new Callback.CommonCallback<List<AgreementInfo>>() {
            @Override
            public void onSuccess(List<AgreementInfo> result) {
                view.onAgreementInfo(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取历史评估列表
     * @param pageNumber        页码
     * @param organId           组织架构id
     * @param agreementId       评估方案所用测试协议id
     * @param beEvaluatedName   被评估人姓名
     * @param betaSchemeId      评估方案所用测试方案id
     * @param sortType          评估日期查询排序，默认倒序排列，SortType此类有两个值： ASC正序排列,DESC倒序排列;
     */
    public void getHistoryEval(int pageNumber, Long organId, Long agreementId,
                               String beEvaluatedName, Long betaSchemeId, SortType sortType){
        Log.d(TAG, "getHistoryEval: 获取历史评估列表");
        
        UserInfo userInfo = RepositoryFactory.getInstance().getUserSource().getUserInfo();
        Map<String, Object> map = new HashMap<>();
        map.put("pageNumber", pageNumber);
        map.put("pageSize", 10);

        if (organId != null){
            map.put("organId", organId);
        }
        if (agreementId != null){
            map.put("agreementId", agreementId);
        }
        if (!StringUtils.isEmpty(beEvaluatedName)){
            map.put("beEvaluatedName", beEvaluatedName);
        }
        if (betaSchemeId != null){
            map.put("betaSchemeId", betaSchemeId);
        }
        if (sortType != null){
            map.put("sortType", sortType);
        }
        evalSource.getHistoryEval(map, new Callback.CommonCallback<List<EvalHostoryInfo>>() {
            @Override
            public void onSuccess(List<EvalHostoryInfo> result) {
                view.onEvalHostoryInfo(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    public void rateePageList(int pageNumber, Long organId){
        Map<String, Object> map = new HashMap<>();
        map.put("pageNumber", pageNumber);
        map.put("pageSize", 10);
        map.put("organId", organId);
        evalSource.rateePageList(map, new Callback.CommonCallback<List<TestUserInfo>>() {
            @Override
            public void onSuccess(List<TestUserInfo> result) {
                view.onTestUserInfo(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取测试协议
     */
    public void getEvalDetail(Integer evalId){
        evalSource.getEvalDetail(evalId, new Callback.CommonCallback<EvalDetail>() {
            @Override
            public void onSuccess(EvalDetail result) {
                view.onEvalDetail(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 获取功率
     * @param userId
     */
    public void calculatePower(Integer userId){
        evalSource.calculatePower(userId, new Callback.CommonCallback<Double>() {
            @Override
            public void onSuccess(Double result) {
                view.onPower(result);
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }

    /**
     * 创建评估
     *| 属性             | 类型   | 是否必传 | 含义                            |
     * | ---------------- | ------ | -------- | ------------------------------- |
     * | name             | String | 是       | 评估方案名称                    |
     * | organId          | Long   | 是       | 评估方案所属组织                |
     * | evaluationDate   | String | 是       | 评估日期 yyyy-MM-dd             |
     * | betaSchemeId     | Long   | 是       | 评估方案所选测试方案id          |
     * | agreementId      | Long   | 是       | 评估方案所选测试协议id          |
     * | beEvaluatorIdStr | String | 是       | 被评估人员id列表字符串,逗号分割 |
     * @param name
     */
    public void create(String name, String evaluationDate, Long betaSchemeId,
                       Long agreementId, String beEvaluatorIdStr){

        UserInfo userInfo = RepositoryFactory.getInstance().getUserSource().getUserInfo();
        Long organId = Long.valueOf(userInfo.getOrganId());

        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("organId", organId);
        map.put("evaluationDate", evaluationDate);
        map.put("betaSchemeId", betaSchemeId);
        map.put("agreementId", agreementId);
        map.put("beEvaluatorIdStr", beEvaluatorIdStr);
        evalSource.create(map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                view.onCreateSucess();
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
                if (code.contains("6105")){
                    view.onNameError();
                }
            }
        });
    }

    @Override
    public void start() {

    }

    @Override
    public BaseView getView() {
        return view;
    }
}
